﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AviagePortal
{
    class SearchOperation
    {
        const int rowLimit = 5;
        static List<string> searchTypes = new List<string>(new string[] { "AboutUs", "Media", "Career", "Service", "Product" });
        static List<string>  languages = new List<string>(new string[] { "English", "Chinese" });
        static List<string> deviceTypes = new List<string>(new string[] { "PC", "Mobile" });
        public static SearchResultSummary Search(string searchText, string searchType, string language, string deviceType)
        {
             
            SearchResultSummary result = new SearchResultSummary();
            result.Items = new List<SearchResultEntity>();
            if (string.IsNullOrWhiteSpace(searchText))
            {
                result.ErrorMessage = "searchText can't be null";
                return result;
            }

            if (string.IsNullOrWhiteSpace(searchType))
            {
                result.ErrorMessage = "searchType can't be null";
                return result;
            }

            if (!searchTypes.Contains(searchType))
            {
                result.ErrorMessage = "searchType only can be \"AboutUs\", \"Media\", \"Career\", \"Service\", \"Product\"";
                return result;
            }

            if (string.IsNullOrWhiteSpace(language))
            {
                result.ErrorMessage = "language can't be null";
                return result;
            }

            if (!languages.Contains(language))
            {
                result.ErrorMessage = "searchType only can be \"English\", \"Chinese\"";
                return result;
            }

            if (string.IsNullOrWhiteSpace(deviceType))
            {
                result.ErrorMessage = "deviceType can't be null";
                return result;
            }

            if (!deviceTypes.Contains(deviceType))
            {
                result.ErrorMessage = "deviceType only can be \"PC\", \"Mobile\"";
                return result;
            }

            if (searchType == "AboutUs")
            {
                SearchAboutUs(searchText, language, deviceType, result);
            }
            else if (searchType == "Media")
            {
                SearchMedia(searchText, language, deviceType, result);
            }
            else if (searchType == "Career")
            {
                SearchCareer(searchText, language, deviceType, result);
            }
            else if (searchType == "Service")
            {
                SearchService(searchText, language, deviceType, result);
            }
            else if (searchType == "Product")
            {
                SearchProduct(searchText, language, deviceType, result);
            }
           
            return result;
        }

        public static SearchResultSummary Search(string searchText, string language, string deviceType)
        {
            SearchResultSummary result = new SearchResultSummary();
            result.Items = new List<SearchResultEntity>();
            if (string.IsNullOrWhiteSpace(searchText))
            {
                result.ErrorMessage = "searchText can't be null";
                return result;
            }         

            if (string.IsNullOrWhiteSpace(language))
            {
                result.ErrorMessage = "language can't be null";
                return result;
            }

            if (!languages.Contains(language))
            {
                result.ErrorMessage = "searchType only can be \"English\", \"Chinese\"";
                return result;
            }

            if (string.IsNullOrWhiteSpace(deviceType))
            {
                result.ErrorMessage = "deviceType can't be null";
                return result;
            }

            if (!deviceTypes.Contains(deviceType))
            {
                result.ErrorMessage = "deviceType only can be \"PC\", \"Mobile\"";
                return result;
            }

            SearchProduct(searchText, language, deviceType, result);
            SearchAboutUs(searchText, language, deviceType, result);
            SearchCareer(searchText, language, deviceType, result);
            SearchMedia(searchText, language, deviceType, result);
            SearchService(searchText, language, deviceType, result);

            return result;
        }


        public static void SearchProduct(string searchText, string language, string deviceType, SearchResultSummary SearchResult)
        { 
            // Search product 
            SPList productList = SPContext.Current.Web.Lists["Product"];
            SPQuery productQuery = new SPQuery();
            string caml = @"<Where>
                          <And>
                            <And>     
                                <Or>
                                  <Or>
                                    <Or>                                   
                                      <Contains>
                                        <FieldRef Name='ArticleTitle'/>
                                        <Value Type='Text' >{0}</Value>
                                      </Contains>
                                      <Contains>
                                        <FieldRef Name='Title'/>
                                        <Value Type='Text' >{0}</Value>
                                      </Contains>
                                    </Or>
                                    <Contains>
                                      <FieldRef Name='Feature'/>
                                      <Value Type='Notes' >{0}</Value>
                                    </Contains>
                                  </Or>
                                  <Contains>
                                    <FieldRef Name='Introduction'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>
                                </Or>                                      
                              <Eq>
                                <FieldRef Name='DeviceType'/>
                                <Value Type='Choice' >{2}</Value>
                              </Eq>
                            </And>
                            <Eq>
                              <FieldRef Name='Language'/>
                              <Value Type='Choice' >{1}</Value>
                            </Eq>
                          </And>
                        </Where>";

            caml = string.Format(caml, searchText.Trim(), language.Trim(), deviceType.Trim());
            productQuery.Query = caml;
            productQuery.RowLimit = 9999999;

            SPListItemCollection items = productList.GetItems(productQuery);

            foreach (SPListItem item in items)
            {
                SearchResultEntity searchResultEntity = new SearchResultEntity();
                searchResultEntity.Title = "" + item["Title"];
                searchResultEntity.Id = "" + item.ID;
                searchResultEntity.Order = "" + item["Order0"];
                searchResultEntity.Content = "" + item["Introduction"];
                searchResultEntity.Pagetitle = "" + item["ArticleTitle"];// +item["PageTitle"];changed to same with content title by zhenwei's request
                if (("" + item["Feature"]).Contains(searchText.Trim()))
                {
                    searchResultEntity.Content = "" + item["ArticleTitle"] + ":" + item["Feature"];
                }

                searchResultEntity.Picture = "" + item["SmallPicture1"];
                searchResultEntity.Type = "Product";
                searchResultEntity.Url = "/Pages/product.aspx";
                if (language != "English")
                {
                    searchResultEntity.Url = "/Pages/product_cn.aspx";
                }

                SearchResult.Items.Add(searchResultEntity);
                SearchResult.ProductCount++;
                SearchResult.ItemCount++;
            }
        }


        public static void SearchAboutUs(string searchText, string language, string deviceType, SearchResultSummary SearchResult)
        {
            // Search CompanyHistory
            #region Search CompanyHistory
            SPList companyHistoryList = SPContext.Current.Web.Lists["CompanyHistory"];
            SPQuery companyHistoryQuery = new SPQuery();
            // Description1~Description6
            string companyHistoryCaml = @"<Where>
                          <And>
                            <And>    
                              <Or>
                                <Or> 
                                <Or>
                                  <Or>
                                    <Or>
                                    <Or>
                                       <Or>
                                      <Contains>
                                        <FieldRef Name='Description1'/>
                                        <Value Type='Notes' >{0}</Value>
                                      </Contains>
                                      <Contains>
                                        <FieldRef Name='Description2'/>
                                        <Value Type='Notes' >{0}</Value>
                                      </Contains>
                                    </Or>
                                    <Contains>
                                      <FieldRef Name='Description3'/>
                                      <Value Type='Notes' >{0}</Value>
                                    </Contains>
                                  </Or>
                                  <Contains>
                                    <FieldRef Name='Description4'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>
                                </Or> 
                                    <Contains> 
                                    <FieldRef Name='Description5'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>
                                </Or> 
                                    <Contains> 
                                    <FieldRef Name='Description6'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>  
                                </Or>  
                                   <Contains> 
                                    <FieldRef Name='Description'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>  
                                 </Or>
                                    <Contains> 
                                    <FieldRef Name='Title'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>  
                                 </Or>      
                              <Eq>
                                <FieldRef Name='DeviceType'/>
                                <Value Type='Choice' >{2}</Value>
                              </Eq>
                            </And>
                            <Eq>
                              <FieldRef Name='Language'/>
                              <Value Type='Choice' >{1}</Value>
                            </Eq>
                          </And>
                        </Where>";
            
            companyHistoryCaml = string.Format(companyHistoryCaml, searchText.Trim(), language.Trim(), deviceType.Trim());
            companyHistoryQuery.Query = companyHistoryCaml;
            SPListItemCollection companyHistoryItems = companyHistoryList.GetItems(companyHistoryQuery);
            foreach (SPListItem item in companyHistoryItems)
            {
                SearchResultEntity searchResultEntity = new SearchResultEntity();
                searchResultEntity.Title = "" + item["Title"];
                searchResultEntity.Id = "" + item.ID;
                searchResultEntity.Pagetitle = "" + item["PageTitle"];
                searchResultEntity.Content = "" + item["Description1"] + item["Description2"] + item["Description3"] + item["Description4"] + item["Description5"] + item["Description6"];
                if (("" + item["Description"]).ToUpper().Contains(searchText.ToUpper()))
                {
                    searchResultEntity.Content = "" + item["Description"];
                }

                searchResultEntity.Picture = "" + item["SmallPicture1"];
                searchResultEntity.Type = "AboutUs";
                searchResultEntity.Url = "/Pages/aboutus.aspx ";
                if (language != "English")
                {
                    searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                }

                SearchResult.Items.Add(searchResultEntity);
                SearchResult.AboutUsCount++;
                SearchResult.ItemCount++;
            }
            #endregion

            // Search CompanyIntroduce 
            #region Search CompanyIntroduce
            SPList companyIntroduceList = SPContext.Current.Web.Lists["CompanyIntroduce"];
            SPQuery companyIntroduceQuery = new SPQuery();
            string companyIntroduceCaml = @"<Where>
                          <And>
                            <And>                                
                                    <Or>
                                        <Or>
                                      <Contains>
                                        <FieldRef Name='Description1'/>
                                        <Value Type='Notes' >{0}</Value>
                                      </Contains>
                                      <Contains>
                                        <FieldRef Name='Description2'/>
                                        <Value Type='Notes' >{0}</Value>
                                      </Contains>
                                    </Or>                                   
                                     <Contains>
                                        <FieldRef Name='Title'/>
                                        <Value Type='Text' >{0}</Value>
                                      </Contains>
                                </Or>
                              <Eq>
                                <FieldRef Name='DeviceType'/>
                                <Value Type='Choice' >{2}</Value>
                              </Eq>
                            </And>
                            <Eq>
                              <FieldRef Name='Language'/>
                              <Value Type='Choice' >{1}</Value>
                            </Eq>
                          </And>
                        </Where>";

            companyIntroduceCaml = string.Format(companyIntroduceCaml, searchText.Trim(), language.Trim(), deviceType.Trim());
            companyIntroduceQuery.Query = companyIntroduceCaml;
            SPListItemCollection companyIntroduceItems = companyIntroduceList.GetItems(companyIntroduceQuery);
            foreach (SPListItem item in companyIntroduceItems)
            {
                SearchResultEntity searchResultEntity = new SearchResultEntity();
                searchResultEntity.Title = "" + item["Title"];
                searchResultEntity.Id = "" + item.ID;
                searchResultEntity.Pagetitle = "" + item["PageTitle"];
                searchResultEntity.Content = "" + item["Description1"];
                if (("" + item["Description2"]).Contains(searchText.Trim()))
                {
                    searchResultEntity.Content = "" + item["Description2"];
                }

                searchResultEntity.Type = "AboutUs";
                searchResultEntity.Url = "/Pages/aboutus.aspx ";
                if (language != "English")
                {
                    searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                }

                SearchResult.Items.Add(searchResultEntity);
                SearchResult.AboutUsCount++;
                SearchResult.ItemCount++;
            }
            #endregion

            //  Search ContactUs  
            #region Search ContactUs
            ContactUsItem contactItem = JsonGenerator.GetContactUsItem(SPContext.Current.Web, language, deviceType);
            if(contactItem  !=null)
            {
                string content = contactItem.Title + " "+  contactItem.Adress + " " + contactItem.CustomersLabel + " " + contactItem.CustomersMail + " " + contactItem.FaxLabel + " " + contactItem.FaxValue + " " + contactItem.JobSeekersLabel + " " + contactItem.JobSeekersMail + " " + contactItem.MediaLabel + contactItem.MediaMail + " " + contactItem.PhoneLabel + " " + contactItem.PhoneValue + " " + contactItem.SuppliersLabel + " " + contactItem.SuppliersMail;
                if (content.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Id = contactItem.ID;
                    searchResultEntity.Content = content;
                    searchResultEntity.Pagetitle = contactItem.PageTitle;
                    searchResultEntity.Title = contactItem.Title;
                    searchResultEntity.Picture = contactItem.BuildIcon;
                    searchResultEntity.Type = "AboutUs";
                    searchResultEntity.Url = "/Pages/aboutus.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                    }

                    SearchResult.Items.Add(searchResultEntity);
                    SearchResult.AboutUsCount++;
                    SearchResult.ItemCount++;
                }
            }

            #endregion

            // Search LeaderShipDetail  
            #region Search LeaderShipDetail
            LeaderShipMainItem leaderShipMain = JsonGenerator.GetLeaderShips(SPContext.Current.Web, language, deviceType);
            if (leaderShipMain != null)
            {
                foreach (LeaderShipDetailItem leaderShipDetailItem in leaderShipMain.Leaders)
                {
                    if (leaderShipDetailItem.Introduction.ToUpper().Contains(searchText.ToUpper()) ||
                        leaderShipDetailItem.Position.ToUpper().Contains(searchText.ToUpper()) ||
                        leaderShipDetailItem.Name.ToUpper().Contains(searchText.ToUpper()))
                    {
                        SearchResultEntity searchResultEntity = new SearchResultEntity();
                        searchResultEntity.Id = leaderShipDetailItem.ID;
                        searchResultEntity.Content = leaderShipDetailItem.Name + " " + leaderShipDetailItem.Position + " " + leaderShipDetailItem.Introduction;
                        searchResultEntity.Pagetitle = leaderShipMain.PageTitle;
                        searchResultEntity.Title = leaderShipMain.Title;
                        searchResultEntity.Type = "AboutUs";
                        searchResultEntity.Url = "/Pages/aboutus.aspx ";
                        if (language != "English")
                        {
                            searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                        }

                        SearchResult.Items.Add(searchResultEntity);
                        SearchResult.AboutUsCount++;
                        SearchResult.ItemCount++;
                    }
                }
            }
            
            #endregion

            // Search SocialDuty  
            #region Search SocialDuty
            SocialDutyItem socialDutyItem = JsonGenerator.GetSocialDutyItem(SPContext.Current.Web, language, deviceType);
            if (socialDutyItem != null && socialDutyItem.Description.ToUpper().Contains(searchText.ToUpper()))
            {
                SearchResultEntity searchResultEntity = new SearchResultEntity();
                searchResultEntity.Id = socialDutyItem.ID;
                searchResultEntity.Content = socialDutyItem.Description;
                searchResultEntity.Pagetitle = socialDutyItem.PageTitle;
                searchResultEntity.Title = socialDutyItem.Title;
                searchResultEntity.Picture = socialDutyItem.Picture1;
                searchResultEntity.Type = "AboutUs";
                searchResultEntity.Url = "/Pages/aboutus.aspx";
                if (language != "English")
                {
                    searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                }

                SearchResult.Items.Add(searchResultEntity);
                SearchResult.AboutUsCount++;
                SearchResult.ItemCount++;
            }

            #endregion

            // Search PartnerShip    
            #region Search PartnerShip
            PartnerShipItem partnerShipItem = JsonGenerator.GetPartnerShipItem(SPContext.Current.Web, language, deviceType);
            if (partnerShipItem != null &&
                (partnerShipItem.TopDescription.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.BottomDescription.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Left1.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Left2.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Left3.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Right1.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Right2.ToUpper().Contains(searchText.ToUpper()) ||
                partnerShipItem.Right3.ToUpper().Contains(searchText.ToUpper()) ||
                 partnerShipItem.Title.ToUpper().Contains(searchText.ToUpper())
                ))
            {
                SearchResultEntity searchResultEntity = new SearchResultEntity();
                searchResultEntity.Id = partnerShipItem.ID;
                searchResultEntity.Content = partnerShipItem.TopDescription + " " + partnerShipItem.Left1 + " " + partnerShipItem.Left2 + " " + partnerShipItem.Left3 + " " + partnerShipItem.Right1 + " " + partnerShipItem.Right2 + " " + partnerShipItem.Right3  +" " + partnerShipItem.BottomDescription;
                searchResultEntity.Pagetitle = partnerShipItem.PageTitle;
                searchResultEntity.Title = partnerShipItem.Title;                
                searchResultEntity.Type = "AboutUs";
                searchResultEntity.Url = "/Pages/aboutus.aspx ";
                if (language != "English")
                {
                    searchResultEntity.Url = "/Pages/aboutus_cn.aspx";
                }

                SearchResult.Items.Add(searchResultEntity);
                SearchResult.AboutUsCount++;
                SearchResult.ItemCount++;
            }

            #endregion

            // Search Our company

        }


        public static void SearchMedia(string searchText, string language, string deviceType, SearchResultSummary SearchResult)
        {
            // Search NewsDetail  
            #region Search NewsDetail
            List<NewsDetailItem> newsDetailItems = JsonGenerator.GetNewsDetailItems(SPContext.Current.Web, language, deviceType);
            NewsMainItem mainDetailItem = JsonGenerator.GetNewsMainItem(SPContext.Current.Web, language, deviceType);
            bool matchedMiedaQeury = false;
            if (mainDetailItem != null)
            {
                if (mainDetailItem.MediaQueryTitle.ToUpper().Contains(searchText.ToUpper()) || mainDetailItem.MediaQueryContent.ToUpper().Contains(searchText.ToUpper()))
                {
                    matchedMiedaQeury = true;
                }
            }

            if (newsDetailItems.Count > 0)
            {
                foreach (NewsDetailItem newsDetailItem in newsDetailItems)
                {
                    if ((newsDetailItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                        newsDetailItem.SubTitle.ToUpper().Contains(searchText.ToUpper()) ||
                        newsDetailItem.Content.ToUpper().Contains(searchText.ToUpper()) ||
                        newsDetailItem.CompanyAboutContent.ToUpper().Contains(searchText.ToUpper()) ||
                        newsDetailItem.CompanyAboutTitle.ToUpper().Contains(searchText.ToUpper()) ||
                        newsDetailItem.ShortDescription.ToUpper().Contains(searchText.ToUpper())) || matchedMiedaQeury)
                    {
                        SearchResultEntity searchResultEntity = new SearchResultEntity();
                        searchResultEntity.Id = newsDetailItem.ID;
                        searchResultEntity.Url = newsDetailItem.URL;
                        searchResultEntity.Pagetitle = newsDetailItem.Title;
                        searchResultEntity.Title = newsDetailItem.Title;                     
                        searchResultEntity.Content = newsDetailItem.Title + " " + newsDetailItem.SubTitle + " " + newsDetailItem.Content + " " + newsDetailItem.CompanyAboutTitle + " " + newsDetailItem.CompanyAboutContent + " " + newsDetailItem.ShortDescription;
                       if (matchedMiedaQeury)
                        {
                            searchResultEntity.Content = mainDetailItem.Title + " " + mainDetailItem.MediaQueryContent;
                        }

                        searchResultEntity.Order = newsDetailItem.Order.ToString();
                        searchResultEntity.Picture = newsDetailItem.Picture;
                        searchResultEntity.Type = "Media";
                        SearchResult.MediaCount++;
                        SearchResult.ItemCount++;
                        SearchResult.Items.Add(searchResultEntity);
                    }
                }
            }

            #endregion
            // Search MediaDetail
            #region Search MediaDetail
            MediaCenterItem mediaCenterItem = JsonGenerator.GetMediaCenterItem(SPContext.Current.Web, language, deviceType);
            if (mediaCenterItem != null)
            {
                foreach (MediaDetailItem detailItem in mediaCenterItem.MediaItems)
                {
                    if (detailItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                        detailItem.Description.ToUpper().Contains(searchText.ToUpper()))
                    {
                        SearchResultEntity searchResultEntity = new SearchResultEntity();
                        searchResultEntity.Id = detailItem.ID;
                        searchResultEntity.Pagetitle = detailItem.Title;// mediaCenterItem.PageTitle; changed to same with content title by zhenwei's request

                        searchResultEntity.Content = detailItem.Title + " " + detailItem.Description;
                        searchResultEntity.Picture = detailItem.Picture;
                        searchResultEntity.Order = detailItem.Order.ToString();
                        searchResultEntity.Title = detailItem.Title;
                        searchResultEntity.Picture = detailItem.Picture;
                      
                        searchResultEntity.Url = "/Pages/media2.aspx";
                        if (language != "English")
                        {
                            searchResultEntity.Url = "/Pages/media2_cn.aspx";
                        }
                        searchResultEntity.Type = "Media";
                        SearchResult.MediaCount++;
                        SearchResult.ItemCount++;
                        SearchResult.Items.Add(searchResultEntity);
                    }
                }
            }
           
            #endregion
        }

        public static void SearchCareer(string searchText, string language, string deviceType, SearchResultSummary SearchResult)
        {
            // Search Career  
            #region Search Career
            CareerItem careerItem = JsonGenerator.GetCareerItem(SPContext.Current.Web, language, deviceType);
            if (careerItem != null)
            {
                if(careerItem.Title.ToUpper().Contains(searchText.ToUpper()) || 
                    careerItem.Description.ToUpper().Contains(searchText.ToUpper()) ||
                    careerItem.CareerPath1.ToUpper().Contains(searchText.ToUpper()) ||
                    careerItem.CareerPath3.ToUpper().Contains(searchText.ToUpper()) ||                   
                careerItem.CareerPath2.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Id = careerItem.ID;
                    searchResultEntity.Pagetitle = careerItem.PageTitle;

                    searchResultEntity.Content = careerItem.Title + " " + careerItem.CareerPath1 + " " + careerItem.CareerPath2 + " " + careerItem.CareerPath3 + " " + careerItem.Description;
                    searchResultEntity.Picture = careerItem.CareerPathPicture;
                    searchResultEntity.Title = careerItem.Title;
                    searchResultEntity.Pagetitle = careerItem.PageTitle;

                    searchResultEntity.Url = "/Pages/career.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/career_cn.aspx";
                    }
                    searchResultEntity.Type = "Career";
                    SearchResult.CareerCount++;
                    SearchResult.ItemCount++;
                    SearchResult.Items.Add(searchResultEntity);
                }
            }
            #endregion

            // Search Training
            #region Search Training
            TrainingItem trainingItem = JsonGenerator.GetTrainingItem(SPContext.Current.Web, language, deviceType);
            if (trainingItem != null)
            {
                if (trainingItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                    trainingItem.Description.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Id = trainingItem.ID;
                    searchResultEntity.Pagetitle = trainingItem.PageTitle;
                    searchResultEntity.Title = trainingItem.Title;

                    searchResultEntity.Content = trainingItem.Title + " " + trainingItem.Description;
                    searchResultEntity.Picture = trainingItem.Picture;

                    searchResultEntity.Url = "/Pages/career.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/career_cn.aspx";
                    }
                    searchResultEntity.Type = "Career";
                    SearchResult.CareerCount++;
                    SearchResult.ItemCount++;
                    SearchResult.Items.Add(searchResultEntity);
                }
            }
            #endregion

            // Search OurLife
            #region Search OurLife
            OurLifeItem ourLifeItem = JsonGenerator.GetOurLifeItem(SPContext.Current.Web, language, deviceType);
            if (ourLifeItem != null)
            {
                if (ourLifeItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                    ourLifeItem.Description.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Id = ourLifeItem.ID;
                    searchResultEntity.Pagetitle = ourLifeItem.PageTitle;
                    searchResultEntity.Title = ourLifeItem.Title;
                    searchResultEntity.Content = ourLifeItem.Title + " " + ourLifeItem.Description;
                    searchResultEntity.Picture = ourLifeItem.Pictures.Count > 0 ? ourLifeItem.Pictures[0] : null;

                    searchResultEntity.Url = "/Pages/career.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/career_cn.aspx";
                    }
                    searchResultEntity.Type = "Career";
                    SearchResult.CareerCount++;
                    SearchResult.ItemCount++;
                    SearchResult.Items.Add(searchResultEntity);
                }
            }
            #endregion

            // Search Opportunities 
            #region Search Opportunities
            OpportunitiesItem opportunitiesItem = JsonGenerator.GetOpportunitiesItem(SPContext.Current.Web, language, deviceType);
            if (opportunitiesItem != null)
            {
                if (opportunitiesItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.Description.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobDescription1.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobDescription2.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobTitle1.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobTitle2.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Id = opportunitiesItem.ID;
                    searchResultEntity.Pagetitle = opportunitiesItem.PageTitle;
                    searchResultEntity.Title = opportunitiesItem.Title;

                    if (opportunitiesItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.Description.ToUpper().Contains(searchText.ToUpper()))
                    {
                        searchResultEntity.Content = opportunitiesItem.Title + " " + opportunitiesItem.Description;
                    }
                    else if (opportunitiesItem.JobDescription1.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobTitle1.ToUpper().Contains(searchText.ToUpper()))
                    {
                        searchResultEntity.Content = opportunitiesItem.JobTitle1 + " " + opportunitiesItem.JobDescription1;
                    }
                    else if (opportunitiesItem.JobDescription2.ToUpper().Contains(searchText.ToUpper()) ||
                    opportunitiesItem.JobTitle2.ToUpper().Contains(searchText.ToUpper()))
                    {
                        searchResultEntity.Content = opportunitiesItem.JobTitle2 + " " + opportunitiesItem.JobDescription2;
                    }

                    searchResultEntity.Picture = opportunitiesItem.HotPositionIcon;
                    searchResultEntity.Url = "/Pages/career.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/career_cn.aspx";
                    }
                    searchResultEntity.Type = "Career";
                    SearchResult.CareerCount++;
                    SearchResult.ItemCount++;
                    SearchResult.Items.Add(searchResultEntity);
                }
            }

            #endregion
        }

        public static void SearchService(string searchText, string language, string deviceType, SearchResultSummary SearchResult)
        {
            // Search Service
            #region Search Service
            ServiceItem serviceItem = JsonGenerator.GetServiceItem(SPContext.Current.Web, language, deviceType);
            if (serviceItem != null)
            {
                if (serviceItem.Title.ToUpper().Contains(searchText.ToUpper()) ||
                    serviceItem.Content.ToUpper().Contains(searchText.ToUpper()))
                {
                    SearchResultEntity searchResultEntity = new SearchResultEntity();
                    searchResultEntity.Pagetitle = serviceItem.PageTitle;
                    searchResultEntity.Title = serviceItem.Title;
                    searchResultEntity.Content = serviceItem.Title + " " + serviceItem.Content;
                    searchResultEntity.Picture = serviceItem.BackgroundPicture;

                    searchResultEntity.Url = "/Pages/service.aspx";
                    if (language != "English")
                    {
                        searchResultEntity.Url = "/Pages/service_cn.aspx";
                    }
                    searchResultEntity.Type = "Service";
                    SearchResult.ServiceCount++;
                    SearchResult.ItemCount++;
                    SearchResult.Items.Add(searchResultEntity);
                }
            }

            #endregion
        }

        private static SearchResultSummary GetCurrentPageItems(SearchResultSummary searchResult, int pageNumber)
        {
            SearchResultSummary newResult = new SearchResultSummary();
            newResult.SearchText = searchResult.SearchText;
            newResult.ItemCount = searchResult.ItemCount;
            newResult.PageCount = searchResult.PageCount;
            newResult.CurrentPage = searchResult.CurrentPage;
            newResult.ProductCount = searchResult.ProductCount;
            newResult.AboutUsCount = searchResult.AboutUsCount;
            newResult.CareerCount = searchResult.CareerCount;
            newResult.MediaCount = searchResult.MediaCount;
            newResult.ServiceCount = searchResult.ServiceCount;

            if (pageNumber > searchResult.PageCount || pageNumber <= 0)
            {
                newResult.ErrorMessage = "Page number is out of range";
            }
            else
            {
                int startIndex = (pageNumber - 1) * rowLimit;
                int endIndex = pageNumber * rowLimit < searchResult.ItemCount ? pageNumber * rowLimit -1 : searchResult.ItemCount;

                for (int i = startIndex; i < endIndex; i++)
                {
                    newResult.Items.Add(searchResult.Items[i]);
                }
            }

            return newResult;
        }
    }

    class SearchResultEntity
    {
        // product, aboutus, careers, news, media, service
        public string Type{get; set;}

        public string Pagetitle { get; set; }

        public string Title { get; set; }

        public string Id { get; set; }

        public string Order { get; set; }

        public string Url { get; set; }

        public string Content { get; set; }

        public string Picture { get; set; }
    }

    class SearchResultSummary {
        public  List<SearchResultEntity> Items { get; set; }
        public string SearchText { get; set; }

        public int ItemCount { get; set; }

        public int PageCount { get; set; }

        public int CurrentPage { get; set; }

        public int ProductCount { get; set; }

        public int AboutUsCount { get; set; }

        public int CareerCount { get; set; }

        public int MediaCount { get; set; }

        public int ServiceCount { get; set; }

        public string ErrorMessage { get; set; }
    }
}
